<template>
  <van-popup class="bank_list_popup" v-model="isShow" duration="0.4"
             position="bottom">
    <div class="header">
      <Header title="开户行名称" :isBack="false" @leftClick="leftClick"></Header>
    </div>
    <div class="content_box">
      <div class="search_box">
        <van-search
          v-model="bankName"
          show-action
          placeholder="请输入搜索关键词"
          @search="onSearch"
          @cancel="onCancel"
        />
      </div>
      <div class="content_list">
        <van-cell v-for="(item, index) in list" :key="index"
                  :value="item.name" @click="checkBank(item)" />
      </div>
    </div>
  </van-popup>
</template>

<script>
import { Popup, Search } from 'vant';
import Header from '@/components/Header.vue';
import mockData from '@/libs/mockData';

export default {
  components: {
    [Popup.name]: Popup,
    [Search.name]: Search,
    Header
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    bankName(val) {
      if (!val) {
        this.list = mockData.banks;
        return;
      }
      const list = [];
      const patt = new RegExp(val, 'g');
      mockData.banks.forEach((value, index) => {
        if (patt.test(value.name)) {
          list.push(mockData.banks[index]);
        }
        patt.exec(value);
      });
      this.list = list;
    }
  },
  data() {
    return {
      bankName: '',
      list: []
    };
  },
  methods: {
    leftClick() {
      this.$emit('update:is-show', false);
    },
    checkBank(bankInfo) {
      this.$emit('update:is-show', false);
      this.$emit('select', bankInfo);
    },
    onSearch() {
      console.log(this.list);
    },
    onCancel() {
      this.$emit('update:is-show', false);
      console.log(mockData.banks);
    }
  },
  created() {
    this.list = mockData.banks;
  },
  mounted() {}
};
</script>

<style lang="less">
.bank_list_popup {
  height: 100%;
  .search_box{
    .van-field__control{
      text-align: left;
    }
  }
  .content_box{
    .content_list{
      /*background-color: #FFF;*/
    }
  }
}
</style>
